package com.zhiwang.planet.mvvm.viewmodel

import android.content.Context
import android.content.Intent
import android.text.TextUtils
import android.view.View
import com.zhiwang.planet.R
import com.zhiwang.planet.event.TabChangeEvent
import com.zhiwang.planet.ui.MembershipActivity
import com.zhiwang.planet.ui.WebViewActivity
import com.zhiwang.planet.ui.gamebase.GameBaseActivity
import com.zhiwang.planet.ui.my.BindPhoneActivity
import com.zhiwang.planet.ui.my.MyDetailActivity
import com.zhiwang.planet.ui.my.withdraw.WithDrawRecordFragment
import com.zhiwang.planet.util.AppUtil
import com.zhiwang.planet.util.Store
import com.zhiwang.planet.util.TokenManager
import com.zhiwang.planet.widget.SimpleDialogBuilder
import org.greenrobot.eventbus.EventBus

class RecommendItemViewModel(
        val type: Int,
        val title: String,
        val description: String,
        val iconRes: Int,
        val isRecomm: Boolean,
        val link: String? = null,
        var iconUrl: String = "",
        var native: Int?,
        var nativePageId: Int?,
        var phoneBind: Boolean = false
        /*超链接等等属性*/) {

    fun onItemClick(v: View) {

        if (type == 7 && TextUtils.isEmpty(link).not()) {
            WebViewActivity.load(v.context, link!!)
            return
        }

        //原生页面跳转
        if (native == 1) {
            val intent = Intent()
            when (nativePageId) {
                //游戏基地页面
                5 -> intent.setClass(v.context, GameBaseActivity::class.java)
                //提现页面
                6 -> intent.setClass(v.context, WithDrawRecordFragment::class.java)
                //我的明细
                7 -> intent.setClass(v.context, MyDetailActivity::class.java)
                //vip 页面
                8 -> intent.setClass(v.context, MembershipActivity::class.java)
                else -> return
            }

            v.context.startActivity(intent)

            return
        }


        //之前的 h5页面或者是 首页tab切换

        if (type == 3) {//type 3 固定是活动 ，且不需要登录
            WebViewActivity.load(v.context, Store.URL_ACTIVITY)
            return
        }

        if (type == 5) { //日常任务
            //其他页面切换需要登录
            if (TokenManager.isLoginValid().not()) {
                AppUtil.onAuthExpired(v.context)
                return
            }
            if (phoneBind.not()) {
                showBindPhoneAlertDialog(v.context)
                return
            }
            EventBus.getDefault().post(TabChangeEvent(2))
//            WebViewActivity.load(v.context, Store.URL_DAILY_TASKS)
            return
        }

        if (type == 6) {
            WebViewActivity.loadImmersive(context = v.context, url = Store.URL_FRIEND_INVITE)
            return
        }


        //其他页面切换需要登录
        if (TokenManager.isLoginValid().not()) {
            AppUtil.onAuthExpired(v.context)
            return
        }

        //type  =0 .invalid type for loading raw url
        if (link != null && link.isNotEmpty()) {
            WebViewActivity.load(v.context, link)
            return
        }
        when (type) {
            2, 3 -> EventBus.getDefault().post(TabChangeEvent(2))
            4 -> EventBus.getDefault().post(TabChangeEvent(1))
        }
    }

    private fun showBindPhoneAlertDialog(context: Context) {
        SimpleDialogBuilder()
                .title("提示")
                .titleTextColor(R.color.colorPrimary)
                .buttonText("我知道了")
                .content("请先绑定手机号")
                .onButtonClick(View.OnClickListener {
                    context.startActivity(Intent(context, BindPhoneActivity::class.java))
                })
                .build(context)
                .show()
    }
}